package com.example.cloud.config;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.baomidou.mybatisplus.core.injector.ISqlInjector;
import com.baomidou.mybatisplus.extension.injector.LogicSqlInjector;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;

/**
 * MyBatis Plus配置。<br>
 * http://mp.baomidou.com/guide/
 *
 * @author tongyufu
 * @since 2018年11月14日 下午4:07:31
 */
@Configuration
@EnableTransactionManagement
@MapperScan(basePackages = "com.example.cloud.dao")
public class MyBatisPlusConfiguration {

    /**
     * 启用逻辑删除。 http://mp.baomidou.com/guide/logic-delete.html
     *
     * @return LogicSqlInjector
     */
    @Bean
    public ISqlInjector sqlInjector() {
        return new LogicSqlInjector();
    }

    /**
     * 分页插件。 http://mp.baomidou.com/guide/page.html
     *
     * @return PaginationInterceptor
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }

    /**
     * 性能分析拦截器，用于输出每条 SQL 语句及其执行时间。<br/>
     * 该插件只用于开发环境，不建议生产环境使用。<br/>
     * http://mp.baomidou.com/guide/performance-analysis-plugin.html
     *
     * @return performanceInterceptor
     */
    @Bean
    @Profile({ "dev", "test" })
    public PerformanceInterceptor performanceInterceptor() {
        PerformanceInterceptor interceptor = new PerformanceInterceptor();
        // SQL是否格式化 默认false
        interceptor.setFormat(true);
        //SQL 执行最大时长，超过自动停止运行，有助于发现问题。
        interceptor.setMaxTime(100);
        return interceptor;
    }
}
